/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cong.timesheet.action;

import com.cong.timesheet.form.UserForm;
import com.cong.timesheet.model.User;
import com.cong.timesheet.service.UserService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Ram
 */
public class EditUserAction extends Action{

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        UserForm userForm=(UserForm)form;
        UserService userService=new UserService();
        //userService.edit(userForm.getId(),userForm.getUserName(),userForm.getPassword(),userForm.getFirstName(),userForm.getLastName(),userForm.getEmail());
        User user=new User(userForm.getId(),userForm.getUserName(),userForm.getPassword(),userForm.getFirstName(),userForm.getLastName(),userForm.getEmail());
        //userService.addUser(user);
        long userid=user.getId();
        List<User> listUser=userService.getUsers();
        //List<User> list1=userService.getUsers();
        for(User user1:listUser)
        {
            if(user1.getId()==userid){
                user1.setUserName(userForm.getUserName());
                user1.setPassword(userForm.getPassword());
                user1.setFirstName(userForm.getFirstName());
                user1.setLastName(userForm.getLastName());
                user1.setEmail(userForm.getEmail());
                //listUser.remove(user1);
                //listUser.add(user);break;
            }
        }
       
       request.setAttribute("userlist",listUser);
        
        return mapping.findForward("success");
       
      
    }
    
    
    
}
